﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using Controller;
using Controller.DataStore;

namespace UxNumberSorter
{
    partial class uxNumberSortViewer
    {
        #region Fields
        //  Fields

        List<int> randoms;

        //  delegate to sync UI thread
        delegate void UpdateSorterSelector(SortInfo sortInfo);

        Dictionary<string, SortInfo> sortInfos;
        #endregion

        #region Functions
        //  Functions

        //  initializes collections
        void InitializeCollection(string seqSize)
        {
            //  initialize sortInfos collection
            sortInfos = new Dictionary<string, SortInfo>( );

            Cursor current = this.Cursor;
            int seqSz;

            try
            {
                //  clear unsorted sequence view
                uxUnsortedSequenceView.DataSource = null;

                //  set wait cursor for extended operation
                this.Cursor = Cursors.WaitCursor;

                if (Int32.TryParse(seqSize, out seqSz))
                {
                    randoms = Randoms.GetRandoms(seqSz).ToList( );
                }
                else
                {
                    ShowDialog("Please enter a valid integer value.");
                    uxSequenceSize.Text = String.Empty;
                    randoms = null;
                }
            }
            catch (Exception ex)
            {
                ShowDialog(ex.Message);
            }
            finally
            {
                SetEnabledControls( randoms != null );
                this.Cursor = current;
            }
        }

        //  set enabled property for controls dependent on randoms collection
        void SetEnabledControls( bool enabled )
        {
            uxShowUnsortedSequence.Enabled = enabled;
            uxCallControllerSort.Enabled = enabled;
        }

        //  begins sorting
        void SortSequence( )
        {
            uxCompletedSortsSelector.DataSource = null;
            SortController.StartSorters( );
        }

        //  display selected sort info
        void SelectSortInfo(string sorterName)
        {
            //  clear ListBox
            uxSortedSequenceView.DataSource = null;

            //  clear Label
            uxElapsedMilliseconds.Text = String.Empty;

            //  if, by some chance there is a key displayed
            //  we need to detect that and remove it
            if (!sortInfos.ContainsKey(sorterName))
            {
                sortInfos.Remove(sorterName);
                return;
            }

            //  update display with sort info stats
            //  Label to show elapsed milliseconds
            uxElapsedMilliseconds.Text = string.Format("Elapsed Time: {0}ms", 
                sortInfos[sorterName].Timer.ElapsedMilliseconds);

            DisplaySortedSequence(sorterName);
        }

        //  display sorted sequence if option checked
        void DisplaySortedSequence(string sorterName)
        {
            if (uxCheckViewSortedSeq.Checked)
            {
                //  ListBox to show sorted collection
                uxSortedSequenceView.DataSource = sortInfos[sorterName].Collection;
            }
            else
            {
                uxSortedSequenceView.DataSource = null;
            }
        }

        //  display unsorted sequence
        void DisplayUnsortedSequence( )
        {
            uxUnsortedSequenceView.DataSource = randoms;
        }

        //  update dictionary collection and reset uxCompletedSortsSelector.DataSource
        void UpdateCompletedSortSelector(SortInfo sortInfo)
        {
            if (sortInfos.ContainsKey(sortInfo.Sorter.Key))
            {
                //  if the key already exists, simply update the sort info
                sortInfos[sortInfo.Sorter.Key] = sortInfo;
            }
            else
            {
                //  else, add sort info to collection
                sortInfos.Add(sortInfo.Sorter.Key, sortInfo);
            }

            //  set DataSource
            uxCompletedSortsSelector.DataSource = sortInfos.OrderBy(si => si.Value.Timer.ElapsedMilliseconds)
                                                           .Select(kv => kv.Key)
                                                           .ToList( );

            //  update running sorters status
            //  calling from this method because it is UI synced
            UpdateSorterStatus( );
        }

        //  update running sorters status
        void UpdateSorterStatus( )
        {
            string sortStatus = string.Format("Running Sorters {0}: {1}",
                                              SortController.RunningSorters.Count,
                                              String.Join(", ", SortController.RunningSorters));
            uxRunningSorters.Text = sortStatus;

            //  test to re-enable controls
            bool sortersRunning = SortController.RunningSorters.Count != 0;

            // enable controls
            uxSetSequenceSize.Enabled = !sortersRunning;
            uxCallControllerSort.Enabled = !sortersRunning && (randoms != null);
        }

        //  display general dialog messages
        void ShowDialog(string Message)
        {
            MessageBox.Show(Message);
        }
        #endregion

        #region Properties
        //  Properties

        #endregion
    }
}
